{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Handling A Long Context via `TransformChatHistory`\n",
    "\n",
    "<div class=\"alert alert-danger\" role=\"alert\">\n",
    "    <strong>Deprecation Notice:</strong> <code>TransformChatHistory</code> is no longer supported and will be removed in version <code>0.2.30</code>. Please transition to using <code>TransformMessages</code> as the new standard method. For a detailed introduction to this method, including how to limit the number of tokens in message context history to replace <code>TransformChatHistory</code>, visit our guide <a href=\"https://microsoft.github.io/autogen/docs/topics/handling_long_contexts/intro_to_transform_messages\" target=\"_blank\">Introduction to Transform Messages</a>.\n",
    "</div>\n",
    "\n",
    "This notebook illustrates how you can use the `TransformChatHistory` capability to give any `Conversable` agent an ability to handle a long context. \n",
    "\n",
    "````{=mdx}\n",
    ":::info Requirements\n",
    "Install `pyautogen`:\n",
    "```bash\n",
    "pip install pyautogen\n",
    "```\n",
    "\n",
    "For more information, please refer to the [installation guide](/docs/installation/).\n",
    ":::\n",
    "````"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import autogen\n",
    "from autogen.agentchat.contrib.capabilities import context_handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = {\n",
    "    \"config_list\": [{\"model\": \"gpt-3.5-turbo\", \"api_key\": os.environ.get(\"OPENAI_API_KEY\")}],\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````{=mdx}\n",
    ":::tip\n",
    "Learn more about configuring LLMs for agents [here](/docs/topics/llm_configuration).\n",
    ":::\n",
    "````\n",
    "\n",
    "To add this ability to any agent, define the capability and then use `add_to_agent`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "plot and save a graph of x^2 from -10 to 10\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "To plot and save a graph of the function x^2 from -10 to 10, you can use the matplotlib library in Python. Here is the code:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Calculate corresponding y values (x^2)\n",
      "y = x**2\n",
      "\n",
      "# Create the plot\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Add labels and title\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('y')\n",
      "plt.title('Plot of x^2')\n",
      "\n",
      "# Save the plot as a file\n",
      "plt.savefig('x_squared_plot.png')\n",
      "\n",
      "# Show the plot\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "This code will create a plot of the function x^2 and save it as \"x_squared_plot.png\" in the current directory. Make sure you have the matplotlib library installed before running this code.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Figure(640x480)\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 0 messages. Reduced from 3 to 3.\u001b[0m\n",
      "\u001b[33mTruncated 139 tokens. Tokens reduced from 223 to 84\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480. \n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Generate y values by squaring x\n",
      "y = x ** 2\n",
      "\n",
      "# Plot the graph\n",
      "plt.plot(x, y)\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('x^2')\n",
      "plt.title('Graph of x^2')\n",
      "plt.grid(True)\n",
      "\n",
      "# Save the graph as an image file, for example as 'graph.png'\n",
      "plt.savefig('graph.png')\n",
      "```\n",
      "\n",
      "By executing this updated code, the graph will be saved as an image file named 'graph.png' in the same directory as your Python script.\n",
      "\n",
      "Please let me know if you need any further assistance.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 0 messages. Reduced from 5 to 5.\u001b[0m\n",
      "\u001b[33mTruncated 159 tokens. Tokens reduced from 306 to 147\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Calculate y values (x^2)\n",
      "y = x**2\n",
      "\n",
      "# Plot the graph\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Add labels and title\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('y')\n",
      "plt.title('Graph of x^2')\n",
      "\n",
      "# Save the graph as an image file\n",
      "plt.savefig('graph.png')\n",
      "\n",
      "# Close the plot\n",
      "plt.close()\n",
      "```\n",
      "\n",
      "This code will save the plot as an image file named \"graph.png\" in the current directory. You can change the filename and path if needed.\n",
      "\n",
      "Please let me know if you need any further assistance.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 0 messages. Reduced from 7 to 7.\u001b[0m\n",
      "\u001b[33mTruncated 159 tokens. Tokens reduced from 369 to 210\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Generate y values\n",
      "y = x**2\n",
      "\n",
      "# Plot the graph\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Set labels and title\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('y')\n",
      "plt.title('Graph of x^2')\n",
      "\n",
      "# Save the graph as an image file\n",
      "plt.savefig('graph.png')\n",
      "```\n",
      "\n",
      "This code will save the graph as a PNG image file named 'graph.png' in the same directory where you are running the script. You can change the filename and format (e.g., 'graph.jpg') if desired.\n",
      "\n",
      "Please note that you will need to have the matplotlib library installed to run this code. If you don't have it installed, you can install it by running `pip install matplotlib` in your terminal or command prompt.\n",
      "\n",
      "Let me know if you need any further assistance!\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 0 messages. Reduced from 9 to 9.\u001b[0m\n",
      "\u001b[33mTruncated 198 tokens. Tokens reduced from 471 to 273\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Generate y values by squaring x\n",
      "y = x**2\n",
      "\n",
      "# Plot the graph\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Add labels and title\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('x^2')\n",
      "plt.title('Graph of x^2')\n",
      "\n",
      "# Save the graph as an image file\n",
      "plt.savefig('x_squared_plot.png')\n",
      "\n",
      "# Display the graph\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "This code will save the graph as a PNG image file named \"x_squared_plot.png\" in the current working directory. You can customize the filename and file format according to your needs.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Figure(640x480)\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 1 messages. Reduced from 11 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 174 tokens. Tokens reduced from 501 to 327\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Generate y values\n",
      "y = x ** 2\n",
      "\n",
      "# Create the plot\n",
      "plt.plot(x, y)\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('y')\n",
      "plt.title('Graph of x^2')\n",
      "\n",
      "# Save the plot as an image file\n",
      "plt.savefig('x_squared.png')\n",
      "\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "The code will save the plot as a PNG image file named \"x_squared.png\" in the current directory. You can change the filename or file extension as needed.\n",
      "\n",
      "Please let me know if you need any further assistance!\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Figure(640x480)\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 3 messages. Reduced from 13 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 227 tokens. Tokens reduced from 554 to 327\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "To save the graph as an image file, you can modify the code as follows:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "# Create the plot\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Set the plot title and axes labels\n",
      "plt.title(\"Plot of x^2\")\n",
      "plt.xlabel(\"x\")\n",
      "plt.ylabel(\"y\")\n",
      "\n",
      "# Save the plot as an image file\n",
      "plt.savefig(\"plot.png\")\n",
      "```\n",
      "\n",
      "This code will save the plot as a PNG file with the name \"plot.png\" in the same directory as your Python script. You can change the file name and format (e.g., \"plot.jpg\") if desired.\n",
      "\n",
      "Once you run this modified code, the plot will be saved as an image file. You can then use this file for further analysis or to share the plot with others.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 1 (execution failed)\n",
      "Code output: \n",
      "Traceback (most recent call last):\n",
      "  File \"\", line 4, in <module>\n",
      "    plt.plot(x, y)\n",
      "NameError: name 'x' is not defined\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 5 messages. Reduced from 15 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 258 tokens. Tokens reduced from 622 to 364\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "It seems that the code encountered an error because the variables `x` and `y` were not defined.\n",
      "\n",
      "Let's modify the code to define the variables `x` and `y` and then save the graph as an image file:\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Define the x values as an array from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Define the y values as the square of x\n",
      "y = x ** 2\n",
      "\n",
      "# Create the plot\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Set the plot title and axes labels\n",
      "plt.title(\"Plot of x^2\")\n",
      "plt.xlabel(\"x\")\n",
      "plt.ylabel(\"y\")\n",
      "\n",
      "# Save the plot as an image file\n",
      "plt.savefig('plot.png')\n",
      "```\n",
      "This code uses the `numpy` library to generate an array of x values from -10 to 10 and calculates the corresponding y values as the square of x. The plot is then saved as an image file named `plot.png`.\n",
      "\n",
      "Please make sure you have the `numpy` and `matplotlib` libraries installed before running this code. Let me know if you need help with that.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 7 messages. Reduced from 17 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 401 tokens. Tokens reduced from 765 to 364\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The code executed successfully. The graph was saved as an image file with the name \"plot.png\" in the current working directory.\n",
      "\n",
      "Now you can share the \"plot.png\" file or view it using an image viewer.\n",
      "\n",
      "Let me know if there's anything else I can help with!\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 9 messages. Reduced from 19 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 282 tokens. Tokens reduced from 633 to 351\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Is there anything else I can help you with?\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 11 messages. Reduced from 21 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 342 tokens. Tokens reduced from 634 to 292\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "If you need any further assistance, feel free to ask. I'm here to help!\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_history=[{'content': 'plot and save a graph of x^2 from -10 to 10', 'role': 'assistant'}, {'content': 'To plot and save a graph of the function x^2 from -10 to 10, you can use the matplotlib library in Python. Here is the code:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values from -10 to 10\\nx = np.linspace(-10, 10, 100)\\n\\n# Calculate corresponding y values (x^2)\\ny = x**2\\n\\n# Create the plot\\nplt.plot(x, y)\\n\\n# Add labels and title\\nplt.xlabel(\\'x\\')\\nplt.ylabel(\\'y\\')\\nplt.title(\\'Plot of x^2\\')\\n\\n# Save the plot as a file\\nplt.savefig(\\'x_squared_plot.png\\')\\n\\n# Show the plot\\nplt.show()\\n```\\n\\nThis code will create a plot of the function x^2 and save it as \"x_squared_plot.png\" in the current directory. Make sure you have the matplotlib library installed before running this code.', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nFigure(640x480)\\n', 'role': 'assistant'}, {'content': \"Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480. \\n\\nTo save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values from -10 to 10\\nx = np.linspace(-10, 10, 100)\\n\\n# Generate y values by squaring x\\ny = x ** 2\\n\\n# Plot the graph\\nplt.plot(x, y)\\nplt.xlabel('x')\\nplt.ylabel('x^2')\\nplt.title('Graph of x^2')\\nplt.grid(True)\\n\\n# Save the graph as an image file, for example as 'graph.png'\\nplt.savefig('graph.png')\\n```\\n\\nBy executing this updated code, the graph will be saved as an image file named 'graph.png' in the same directory as your Python script.\\n\\nPlease let me know if you need any further assistance.\", 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\n', 'role': 'assistant'}, {'content': 'Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\\n\\nTo save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values from -10 to 10\\nx = np.linspace(-10, 10, 100)\\n\\n# Calculate y values (x^2)\\ny = x**2\\n\\n# Plot the graph\\nplt.plot(x, y)\\n\\n# Add labels and title\\nplt.xlabel(\\'x\\')\\nplt.ylabel(\\'y\\')\\nplt.title(\\'Graph of x^2\\')\\n\\n# Save the graph as an image file\\nplt.savefig(\\'graph.png\\')\\n\\n# Close the plot\\nplt.close()\\n```\\n\\nThis code will save the plot as an image file named \"graph.png\" in the current directory. You can change the filename and path if needed.\\n\\nPlease let me know if you need any further assistance.', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\n', 'role': 'assistant'}, {'content': \"Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\\n\\nTo save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values\\nx = np.linspace(-10, 10, 100)\\n\\n# Generate y values\\ny = x**2\\n\\n# Plot the graph\\nplt.plot(x, y)\\n\\n# Set labels and title\\nplt.xlabel('x')\\nplt.ylabel('y')\\nplt.title('Graph of x^2')\\n\\n# Save the graph as an image file\\nplt.savefig('graph.png')\\n```\\n\\nThis code will save the graph as a PNG image file named 'graph.png' in the same directory where you are running the script. You can change the filename and format (e.g., 'graph.jpg') if desired.\\n\\nPlease note that you will need to have the matplotlib library installed to run this code. If you don't have it installed, you can install it by running `pip install matplotlib` in your terminal or command prompt.\\n\\nLet me know if you need any further assistance!\", 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\n', 'role': 'assistant'}, {'content': 'Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\\n\\nTo save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values from -10 to 10\\nx = np.linspace(-10, 10, 100)\\n\\n# Generate y values by squaring x\\ny = x**2\\n\\n# Plot the graph\\nplt.plot(x, y)\\n\\n# Add labels and title\\nplt.xlabel(\\'x\\')\\nplt.ylabel(\\'x^2\\')\\nplt.title(\\'Graph of x^2\\')\\n\\n# Save the graph as an image file\\nplt.savefig(\\'x_squared_plot.png\\')\\n\\n# Display the graph\\nplt.show()\\n```\\n\\nThis code will save the graph as a PNG image file named \"x_squared_plot.png\" in the current working directory. You can customize the filename and file format according to your needs.', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nFigure(640x480)\\n', 'role': 'assistant'}, {'content': 'Great! The code executed successfully and generated a plot of the function x^2 from -10 to 10. The plot was displayed in a figure with size 640x480.\\n\\nTo save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Generate x values\\nx = np.linspace(-10, 10, 100)\\n\\n# Generate y values\\ny = x ** 2\\n\\n# Create the plot\\nplt.plot(x, y)\\nplt.xlabel(\\'x\\')\\nplt.ylabel(\\'y\\')\\nplt.title(\\'Graph of x^2\\')\\n\\n# Save the plot as an image file\\nplt.savefig(\\'x_squared.png\\')\\n\\nplt.show()\\n```\\n\\nThe code will save the plot as a PNG image file named \"x_squared.png\" in the current directory. You can change the filename or file extension as needed.\\n\\nPlease let me know if you need any further assistance!', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nFigure(640x480)\\n', 'role': 'assistant'}, {'content': 'To save the graph as an image file, you can modify the code as follows:\\n\\n```python\\nimport matplotlib.pyplot as plt\\n\\n# Create the plot\\nplt.plot(x, y)\\n\\n# Set the plot title and axes labels\\nplt.title(\"Plot of x^2\")\\nplt.xlabel(\"x\")\\nplt.ylabel(\"y\")\\n\\n# Save the plot as an image file\\nplt.savefig(\"plot.png\")\\n```\\n\\nThis code will save the plot as a PNG file with the name \"plot.png\" in the same directory as your Python script. You can change the file name and format (e.g., \"plot.jpg\") if desired.\\n\\nOnce you run this modified code, the plot will be saved as an image file. You can then use this file for further analysis or to share the plot with others.', 'role': 'user'}, {'content': 'exitcode: 1 (execution failed)\\nCode output: \\nTraceback (most recent call last):\\n  File \"\", line 4, in <module>\\n    plt.plot(x, y)\\nNameError: name \\'x\\' is not defined\\n', 'role': 'assistant'}, {'content': 'It seems that the code encountered an error because the variables `x` and `y` were not defined.\\n\\nLet\\'s modify the code to define the variables `x` and `y` and then save the graph as an image file:\\n\\n```python\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\n# Define the x values as an array from -10 to 10\\nx = np.linspace(-10, 10, 100)\\n\\n# Define the y values as the square of x\\ny = x ** 2\\n\\n# Create the plot\\nplt.plot(x, y)\\n\\n# Set the plot title and axes labels\\nplt.title(\"Plot of x^2\")\\nplt.xlabel(\"x\")\\nplt.ylabel(\"y\")\\n\\n# Save the plot as an image file\\nplt.savefig(\\'plot.png\\')\\n```\\nThis code uses the `numpy` library to generate an array of x values from -10 to 10 and calculates the corresponding y values as the square of x. The plot is then saved as an image file named `plot.png`.\\n\\nPlease make sure you have the `numpy` and `matplotlib` libraries installed before running this code. Let me know if you need help with that.', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\n', 'role': 'assistant'}, {'content': 'Great! The code executed successfully. The graph was saved as an image file with the name \"plot.png\" in the current working directory.\\n\\nNow you can share the \"plot.png\" file or view it using an image viewer.\\n\\nLet me know if there\\'s anything else I can help with!', 'role': 'user'}, {'content': '', 'role': 'assistant'}, {'content': 'Is there anything else I can help you with?', 'role': 'user'}, {'content': '', 'role': 'assistant'}, {'content': \"If you need any further assistance, feel free to ask. I'm here to help!\", 'role': 'user'}], summary=\"If you need any further assistance, feel free to ask. I'm here to help!\", cost=({'total_cost': 0.015855, 'gpt-3.5-turbo-0613': {'cost': 0.015855, 'prompt_tokens': 8242, 'completion_tokens': 1746, 'total_tokens': 9988}}, {'total_cost': 0.0147465, 'gpt-3.5-turbo-0613': {'cost': 0.0147465, 'prompt_tokens': 7755, 'completion_tokens': 1557, 'total_tokens': 9312}}), human_input=[])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assistant = autogen.AssistantAgent(\n",
    "    \"assistant\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "\n",
    "# Instantiate the capability to manage chat history\n",
    "manage_chat_history = context_handling.TransformChatHistory(max_tokens_per_message=50, max_messages=10, max_tokens=1000)\n",
    "# Add the capability to the assistant\n",
    "manage_chat_history.add_to_agent(assistant)\n",
    "\n",
    "user_proxy = autogen.UserProxyAgent(\n",
    "    \"user_proxy\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",
    "    code_execution_config={\n",
    "        \"work_dir\": \"coding\",\n",
    "        \"use_docker\": False,\n",
    "    },\n",
    "    max_consecutive_auto_reply=10,\n",
    ")\n",
    "\n",
    "user_proxy.initiate_chat(assistant, message=\"plot and save a graph of x^2 from -10 to 10\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Why is this important?\n",
    "This capability is especially useful if you expect the agent histories to become exceptionally large and exceed the context length offered by your LLM.\n",
    "For example, in the example below, we will define two agents -- one without this ability and one with this ability.\n",
    "\n",
    "The agent with this ability will be able to handle longer chat history without crashing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "plot and save a graph of x^2 from -10 to 10\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "Encountered an error with the base assistant\n",
      "Error code: 400 - {'error': {'message': \"This model's maximum context length is 4097 tokens. However, your messages resulted in 1009487 tokens. Please reduce the length of the messages.\", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}\n",
      "\n",
      "\n",
      "\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "plot and save a graph of x^2 from -10 to 10\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 1991 messages. Reduced from 2001 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 1000800 tokens. Tokens reduced from 1001015 to 215\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Here's the Python code to plot and save a graph of x^2 from -10 to 10:\n",
      "\n",
      "```python\n",
      "# filename: plot_graph.py\n",
      "\n",
      "import matplotlib.pyplot as plt\n",
      "import numpy as np\n",
      "\n",
      "# Generate x values from -10 to 10\n",
      "x = np.linspace(-10, 10, 100)\n",
      "\n",
      "# Calculate y values as x^2\n",
      "y = x**2\n",
      "\n",
      "# Create plot\n",
      "plt.plot(x, y)\n",
      "\n",
      "# Add labels and title\n",
      "plt.xlabel('x')\n",
      "plt.ylabel('y')\n",
      "plt.title('Graph of y = x^2')\n",
      "\n",
      "# Save the plot as a PNG image\n",
      "plt.savefig('graph.png')\n",
      "\n",
      "# Show the plot\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "To execute this code, save it to a file called `plot_graph.py` and run it using Python. This will generate a file called `graph.png` in the same directory, which will contain the graph of x^2 from -10 to 10.\n",
      "\n",
      "Note: Make sure you have the matplotlib library installed. You can install it by running `pip install matplotlib` in your terminal or command prompt.\n",
      "\n",
      "Let me know if you need any further assistance!\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Figure(640x480)\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 1993 messages. Reduced from 2003 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 997232 tokens. Tokens reduced from 997466 to 234\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! The graph of x^2 from -10 to 10 has been plotted and saved successfully. You can find the saved graph as an image file on your computer. \n",
      "\n",
      "Is there anything else I can help you with?\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mTruncated 1995 messages. Reduced from 2005 to 10.\u001b[0m\n",
      "\u001b[33mTruncated 997096 tokens. Tokens reduced from 997326 to 230\u001b[0m\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "assistant_base = autogen.AssistantAgent(\n",
    "    \"assistant\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "assistant_with_context_handling = autogen.AssistantAgent(\n",
    "    \"assistant\",\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "# suppose this capability is not available\n",
    "manage_chat_history = context_handling.TransformChatHistory(max_tokens_per_message=50, max_messages=10, max_tokens=1000)\n",
    "manage_chat_history.add_to_agent(assistant_with_context_handling)\n",
    "\n",
    "user_proxy = autogen.UserProxyAgent(\n",
    "    \"user_proxy\",\n",
    "    human_input_mode=\"NEVER\",\n",
    "    is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",
    "    code_execution_config={\n",
    "        \"work_dir\": \"coding\",\n",
    "        \"use_docker\": False,\n",
    "    },\n",
    "    max_consecutive_auto_reply=2,\n",
    ")\n",
    "\n",
    "# suppose the chat history is large\n",
    "# Create a very long chat history that is bound to cause a crash\n",
    "# for gpt 3.5\n",
    "long_history = []\n",
    "for i in range(1000):\n",
    "    # define a fake, very long message\n",
    "    assitant_msg = {\"role\": \"assistant\", \"content\": \"test \" * 1000}\n",
    "    user_msg = {\"role\": \"user\", \"content\": \"\"}\n",
    "\n",
    "    assistant_base.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",
    "    assistant_with_context_handling.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",
    "    user_proxy.send(user_msg, assistant_base, request_reply=False, silent=True)\n",
    "    user_proxy.send(user_msg, assistant_with_context_handling, request_reply=False, silent=True)\n",
    "\n",
    "try:\n",
    "    user_proxy.initiate_chat(assistant_base, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False)\n",
    "except Exception as e:\n",
    "    print(\"Encountered an error with the base assistant\")\n",
    "    print(e)\n",
    "    print(\"\\n\\n\")\n",
    "\n",
    "try:\n",
    "    user_proxy.initiate_chat(\n",
    "        assistant_with_context_handling, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False\n",
    "    )\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
